﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition;

namespace EnhancedWebPart
{
    internal class ServiceLocator
    {
        #region Singleton

        private static ServiceLocator instance = null;

        public static ServiceLocator Current
        {
            get
            {
                if (instance == null)
                {
                    instance = new ServiceLocator();
                }

                return instance;
            }
        }

        private ServiceLocator()
        {
        }

        #endregion


        [ImportMany(typeof(IDisplayableClass))]
        public List<IDisplayableClass> DisplayableClasses;

        internal void Initialize(IEnumerable<Assembly> assemblies)
        {
            var catalog = new AggregateCatalog();
            foreach (var assembly in assemblies)
            {
                catalog.Catalogs.Add(new AssemblyCatalog(assembly));
            }

            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }
    }
}
